package com.mach.platform.service;


import com.google.common.collect.Lists;
import com.mach.platform.domain.ProductionState;
import com.mach.platform.domain.UnderclothAutonomousInspectionRecordTable;
import com.mach.platform.repository.UnderclothAutonomousInspectionRecordTableRepo;
import com.mach.platform.utils.springdata.DynamicSpecifications;
import com.mach.platform.utils.springdata.SearchFilter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@Service
public class UnderclothAutonomousInspectionRecordTableServiceImpl implements BaseService<UnderclothAutonomousInspectionRecordTable> {

    @Autowired
    private UnderclothAutonomousInspectionRecordTableRepo underclothAutonomousInspectionRecordTableRepo;

    private static final DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    @Override
    public UnderclothAutonomousInspectionRecordTable save(UnderclothAutonomousInspectionRecordTable underclothAutonomousInspectionRecordTable) {
        return null;
    }

    @Override
    public List<UnderclothAutonomousInspectionRecordTable> insertObjInBatch(List<UnderclothAutonomousInspectionRecordTable> list) {
        return null;
    }

    @Override
    public UnderclothAutonomousInspectionRecordTable update(UnderclothAutonomousInspectionRecordTable underclothAutonomousInspectionRecordTable) {
        return null;
    }

    @Override
    public int delete(Long[] ids) {
        return underclothAutonomousInspectionRecordTableRepo.deleteByIdIn(ids);
    }

    @Override
    public Page<UnderclothAutonomousInspectionRecordTable> list(Pageable pageable, Map params) {
        if (params.size() > 0){
            Object startTime = params.get("startTime");
            Object endTime = params.get("endTime");
            if (ObjectUtils.allNotNull(startTime, endTime)) {
                LocalDateTime ldtStartTime = LocalDateTime.parse(startTime.toString(), df);
                LocalDateTime ldtEndTime = LocalDateTime.parse(endTime.toString(), df);
                Page<UnderclothAutonomousInspectionRecordTable> result = underclothAutonomousInspectionRecordTableRepo.getListByTime(ldtStartTime, ldtEndTime, pageable);
                return result;
            }
        }
        return null;
    }

    @Override
    public UnderclothAutonomousInspectionRecordTable info(Long id) {
        return null;
    }

    public void add(List<UnderclothAutonomousInspectionRecordTable> underclothAutonomous) {
        if (CollectionUtils.isNotEmpty(underclothAutonomous)){
            List<UnderclothAutonomousInspectionRecordTable> newList = Lists.newArrayList();

            underclothAutonomous.forEach(underclothAutonomousInspectionRecordTable -> {
                if (StringUtils.isNotBlank(underclothAutonomousInspectionRecordTable.getSpecs())){
                    if (underclothAutonomousInspectionRecordTable.getTime() == null) {
                        underclothAutonomousInspectionRecordTable.setTime(LocalDateTime.now());
                    }
                    newList.add(underclothAutonomousInspectionRecordTable);
                }
            });
            //补全课长、值班主管、领班、班别
            String sectionChief = null;
            String shiftSupervisor = null;
            String foreman = null;
            String classBan = null;
            for (UnderclothAutonomousInspectionRecordTable underclothAutonomousInspectionRecordTable : underclothAutonomous){
                if (StringUtils.isNotBlank(underclothAutonomousInspectionRecordTable.getSectionChief())){
                    sectionChief = underclothAutonomousInspectionRecordTable.getSectionChief();
                }
                if (StringUtils.isNotBlank(underclothAutonomousInspectionRecordTable.getShiftSupervisor())){
                    shiftSupervisor = underclothAutonomousInspectionRecordTable.getShiftSupervisor();
                }
                if (StringUtils.isNotBlank(underclothAutonomousInspectionRecordTable.getForeman())){
                    foreman = underclothAutonomousInspectionRecordTable.getForeman();
                }
                if (StringUtils.isNotBlank(underclothAutonomousInspectionRecordTable.getClassBan())){
                    classBan = underclothAutonomousInspectionRecordTable.getClassBan();
                }
            }
            for (UnderclothAutonomousInspectionRecordTable underclothAutonomousInspectionRecordTable : underclothAutonomous){
                underclothAutonomousInspectionRecordTable.setSectionChief(sectionChief);
                underclothAutonomousInspectionRecordTable.setShiftSupervisor(shiftSupervisor);
                underclothAutonomousInspectionRecordTable.setForeman(foreman);
                underclothAutonomousInspectionRecordTable.setClassBan(classBan);
            }
            underclothAutonomousInspectionRecordTableRepo.saveAll(underclothAutonomous);
        }
    }

    public void update(List<UnderclothAutonomousInspectionRecordTable> underclothAutonomousInspectionRecordTables) {
        this.add(underclothAutonomousInspectionRecordTables);
    }

    public List<UnderclothAutonomousInspectionRecordTable> getProductionStateByTime(String time, String classBan) {
        if (StringUtils.isNoneBlank(time, classBan)){
            List<UnderclothAutonomousInspectionRecordTable> result = underclothAutonomousInspectionRecordTableRepo.getProductionStateByTime(time, classBan);
            return result;
        }
        return null;
    }
}
